package net.yakiboo.adrums;

import net.yakiboo.util.*;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;

public class DrumsConnection extends Activity {
	
	private ClientServer cs=new ClientServer();
	private EditText textIP;
	private Button buttonServer, buttonClient, buttonClose;
	private ProgressBar progressBar;
	private DrumsConnection dc=this;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.connection233);
        
        textIP = (EditText)findViewById(R.id.textIP);
        buttonClient = (Button)findViewById(R.id.buttonClient);
        buttonServer = (Button)findViewById(R.id.buttonServer);
        buttonClose = (Button)findViewById(R.id.buttonClose);
        /*
        progressBar = (ProgressBar)findViewById(R.id.progressBar);
        progressBar.setVisibility(ProgressBar.INVISIBLE);
        */
        buttonServer.setOnClickListener(buttonServerOnClickListener);
        buttonClient.setOnClickListener(buttonClientOnClickListener);
        buttonClose.setOnClickListener(buttonCloseOnClickListener);
    }
    
    @Override
    public void onStop(){
    	super.onDestroy();
    	cs.onStop();
    }
    
    public ClientServer getCs() {
		return cs;
	}

	public void setCs(ClientServer cs) {
		this.cs = cs;
	}
	
	private void espera(long time){
		try {
			Thread.sleep(time);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	public boolean OnKeyDown(int keyCode, KeyEvent ke){
		if(keyCode == KeyEvent.KEYCODE_BACK && ke.getRepeatCount()==0){
			ke.startTracking();
			return true;
		}
		return super.onKeyDown(keyCode, ke);
	}
	
	public boolean onKeyUp(int keyCode, KeyEvent ke){
		if(keyCode == KeyEvent.KEYCODE_BACK && ke.isTracking() && !ke.isCanceled()){
			Intent intent = new Intent();
			Bundle bundle = new Bundle();
			bundle.putString("ip", "");
			bundle.putBoolean("isServer", false);
			intent.putExtras(bundle);
			
			dc.setResult(Activity.RESULT_OK, intent);
			dc.finish();
			return true;
		}
		return super.onKeyUp(keyCode, ke);
	}

	Button.OnClickListener buttonServerOnClickListener = new Button.OnClickListener(){
		@Override
		public void onClick(View v) {
			//cs.stopThreads();
			
			cs.loadIP();	
			espera(200);
			
			buttonServer.setText("Servidor: " + cs.getIP());
			//cs.serverJob();
			
			Intent intent = new Intent();
			Bundle bundle = new Bundle();
			bundle.putString("ip", cs.getIP());
			bundle.putBoolean("isServer", true);
			intent.putExtras(bundle);
			
			dc.setResult(Activity.RESULT_OK, intent);
			dc.finish();
		}
    };
    
    
	Button.OnClickListener buttonClientOnClickListener = new Button.OnClickListener(){		
	@Override
	public void onClick(View v) {
		//cs.stopThreads();
		
		buttonServer.setText("Servidor");
		//cs.clientJob(textIP.getText().toString());
		
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		bundle.putString("ip", textIP.getText().toString());
		bundle.putBoolean("isServer", false);
		intent.putExtras(bundle);
		
		dc.setResult(Activity.RESULT_OK, intent);
		dc.finish();
	}};
	
	Button.OnClickListener buttonCloseOnClickListener = new Button.OnClickListener(){		
		@Override
		public void onClick(View v) {
			//cs.stopThreads();
			
			buttonServer.setText("Servidor");
			//cs.clientJob(textIP.getText().toString());
			
			Intent intent = new Intent();
			Bundle bundle = new Bundle();
			bundle.putString("ip", "");
			bundle.putBoolean("isServer", false);
			intent.putExtras(bundle);
			
			dc.setResult(Activity.RESULT_OK, intent);
			dc.finish();
		}};
}
